package com.zc.vertx.demo.first;

import com.zc.vertx.demo.first.bean.Whisky;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by CZC70 on 2017/6/23.
 */
public class MyFirstVerticle extends AbstractVerticle {

    private Map<Integer, Whisky> products = new LinkedHashMap<>();

    private void createSomeData(){
        Whisky bowmore = new Whisky("Bowmore 15 Year Laimring", "Scotlan, Islay");
        products.put(bowmore.getId(),bowmore);
        Whisky talisker = new Whisky("Talisker 57 North", "Scotland, Island");
        products.put(talisker.getId(),talisker);
    }

    @Override
    public void start(Future<Void> startFuture) throws Exception {

        createSomeData();

        Router router = Router.router(vertx);

        router.route("/").handler(routingContext -> {
            HttpServerResponse response = routingContext.response();
            response.putHeader("content-type", "text/html")
                .end("<h1>Hello from my first " +
                        "Vert.x 3application</h1>");
        });

        router.route("/api/whiskies*").handler(BodyHandler.create());
        router.post("/api/whiskies").handler(this::addOne);
        router.get("/api/whiskies").handler(this::getAll);
        router.delete("/api/whiskies/:id").handler(this::deleteOne);
        router.route("/assets/*").handler(StaticHandler.create("assets"));

        vertx.createHttpServer()
                .requestHandler(router::accept)
                .listen(
                    config().getInteger("http.port", 8081),
                    result -> {
                        if(result.succeeded()){
                            startFuture.complete();
                        } else {
                            startFuture.fail(result.cause());
                        }
                    }
                );
    }

    private void getAll(RoutingContext routingContext) {
        routingContext.response()
                .putHeader("content-type", "application/json; charset=utf-8")
                .end(Json.encodePrettily(products.values()));
    }

    private void addOne(RoutingContext routingContext) {
        final Whisky whisky = Json.decodeValue(routingContext.getBodyAsString(),Whisky.class);

        products.put(whisky.getId(),whisky);
        routingContext.response()
                .setStatusCode(201)
                .putHeader("content-type", "application/json; charset=utf-8")
                .end(Json.encodePrettily(whisky));
    }

    private void deleteOne(RoutingContext routingContext) {
        String id = routingContext.request().getParam("id");
        if(StringUtils.isBlank(id)){
            routingContext.response().setStatusCode(400).end();
        } else {
            Integer idAsInteger = Integer.valueOf(id);
            products.remove(idAsInteger);
        }
        routingContext.response().setStatusCode(204).end();
    }


}
